package com.cyou.qb.face.constants;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.util.Assert;

import com.cyou.qb.face.service.SyncGameTask;

public final class FaceConstants implements FaceConstantsMBean {

	/** 接口超时时间*/
	public static int SERVICE_OF_173_TIME_OUT;

	/** 173接口返回值是否需要缓存*/
	public static boolean FACE_CACHE_ABLE = true;

	/** 视频搜索缓存时间（秒）*/
	public static int SEARCH_VIDEO_LIVE_SECOND;

	/** 新闻搜索缓存时间（秒）*/
	public static int SEARCH_NEWS_LIVE_SECOND;

	/**  游戏信息缓存时间（ 秒）*/
	public static int GAME_INFO_LIVE_SECOND;

	/**  游戏频道缓存时间（ 秒）*/
	public static int GAME_CHANNEL_LIVE_SECOND;
	
	public static int PST_LIVE_SECOND=1*60;
	
	

	static Properties pt = null;

	static {
		pt = new Properties();
		InputStream ist = null;
		try {
			ist = FaceConstants.class.getClassLoader().getResourceAsStream("qb-face-config.properties");
			pt.load(ist);
			try {
				SERVICE_OF_173_TIME_OUT = Integer.parseInt(pt.getProperty("SERVICE_OF_173_TIME_OUT"));
				FACE_CACHE_ABLE = Boolean.parseBoolean(pt.getProperty("FACE_CACHE_ABLE"));
				SEARCH_VIDEO_LIVE_SECOND = Integer.parseInt(pt.getProperty("SEARCH_VIDEO_LIVE_SECOND"));
				SEARCH_NEWS_LIVE_SECOND = Integer.parseInt(pt.getProperty("SEARCH_NEWS_LIVE_SECOND"));
				GAME_INFO_LIVE_SECOND = Integer.parseInt(pt.getProperty("GAME_INFO_LIVE_SECOND"));
				GAME_CHANNEL_LIVE_SECOND = Integer.parseInt(pt.getProperty("GAME_CHANNEL_LIVE_SECOND"));
			} catch (Exception nfe) {
				nfe.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != ist) {
					ist.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	@Override
	public void syncGameType() {
		SyncGameTask bean = getSyncTask();
		bean.syncGameType();
	}

	@Override
	public void syncGameName() {
		SyncGameTask bean = getSyncTask();
		bean.syncGameType();
	}

	private SyncGameTask getSyncTask() {
		try {
			SyncGameTask bean = SyncGameTask.BEAN_FACTORY.getBean(SyncGameTask.class);
			Assert.notNull(bean);
			return bean;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("系统状态不正确,请提供SyncGameTask实现类!");
		}
	}

	private void updateProperties() {
		FileOutputStream fost = null;
		try {
			fost = new FileOutputStream(new File(getClass().getClassLoader().getResource("qb-face-config.properties")
					.getPath()));
			pt.store(fost, null);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fost.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public int getServiceOf173TimeOut() {
		return SERVICE_OF_173_TIME_OUT;
	}

	@Override
	public void setServiceOf173TimeOut(int serviceOf173TimeOut) {
		SERVICE_OF_173_TIME_OUT = serviceOf173TimeOut;
		pt.put("SERVICE_OF_173_TIME_OUT", serviceOf173TimeOut + "");
		updateProperties();
	}

	@Override
	public boolean getFaceCacheAble() {
		return FACE_CACHE_ABLE;
	}

	@Override
	public void setFaceCacheAble(boolean faceCacheAble) {
		FACE_CACHE_ABLE = faceCacheAble;
		pt.put("FACE_CACHE_ABLE", faceCacheAble + "");
		updateProperties();
	}

	@Override
	public int getSearchVideoLiveSecond() {
		return SEARCH_VIDEO_LIVE_SECOND;
	}

	@Override
	public void setSearchVideoLiveSecond(int searchVideoLiveSecond) {
		SEARCH_VIDEO_LIVE_SECOND = searchVideoLiveSecond;
		pt.put("SEARCH_VIDEO_LIVE_SECOND", SEARCH_VIDEO_LIVE_SECOND + "");
		updateProperties();
	}

	@Override
	public int getSearchNewsLiveSecond() {
		return SEARCH_NEWS_LIVE_SECOND;
	}

	@Override
	public void setSearchNewsLiveSecond(int searchNewsLiveSecond) {
		SEARCH_NEWS_LIVE_SECOND = searchNewsLiveSecond;
		pt.put("SEARCH_NEWS_LIVE_SECOND", searchNewsLiveSecond + "");
		updateProperties();
	}

	@Override
	public int getGameInfoLiveSecond() {
		return GAME_INFO_LIVE_SECOND;
	}

	@Override
	public void setGameInfoLiveSecond(int gameInfoLiveSecond) {
		GAME_INFO_LIVE_SECOND = gameInfoLiveSecond;
		pt.put("GAME_INFO_LIVE_SECOND", GAME_INFO_LIVE_SECOND + "");
		updateProperties();
	}

	@Override
	public int getGameChannelLiveSecond() {
		return GAME_CHANNEL_LIVE_SECOND;
	}

	@Override
	public void setGameChannelLiveSecond(int gameChannelLiveSecond) {
		GAME_CHANNEL_LIVE_SECOND = gameChannelLiveSecond;
		pt.put("GAME_CHANNEL_LIVE_SECOND", GAME_CHANNEL_LIVE_SECOND + "");
		updateProperties();
	}

}
